package math;

import java.util.List;


public class LeastSquareMethod implements MEstimate {
	public Double estimate(List<Double> V, List<Double> U) {
		//estimate b in V=b*U
		Double sumUV = 0.0;
		Double sumU2 = 0.0;
		
		for (int i=0; i<U.size(); i++) {
			Double u = U.get(i);
			Double v = V.get(i);
			sumUV += u*v;
			sumU2 += u*u;

		}
		return sumUV/sumU2;
	}
	
	
public Vector2D estimateFull (List<Double> V, List<Double> U) {
	int n = V.size();
	Double sumUV = 0.0;
	Double sumU = 0.0;
	Double sumV = 0.0;
	Double sumU2 = 0.0;
	for (int i=0; i<U.size(); i++) {
		Double u = U.get(i);
		Double v = V.get(i);
		sumUV += u*v;
		sumU2 += u*u;
		sumU  += u;
		sumV  += v;
	}
	Double b1 = (n*sumUV - sumU*sumV)/(n*sumU2 - sumU*sumU);
	return new Vector2D(sumV/n-b1*sumU/n, b1);
}
}
